package es.udc.pfc.davidparedes.web.pages.user;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.shift.Shift;
import es.udc.pfc.davidparedes.model.taskservice.TaskService;
import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.GlobalNames;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.MANAGER_OR_ABOVE)
public class UpdateShift {
    
	@SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private TaskService taskService;

    @Property
    private Date startDate;
    
    @Property
    private Date endDate;
    
    @Property
    private String startTime;
    
    @Property
    private String endTime;
    
    @Property
    private Boolean isRange;
    
    @Property
    private String permanent;
    
    @Property
    private List<String> permanentTypes;
    
    @Property
    private Long updateShiftId;
    
    @Property
    private Long removeShiftId;
    
    @Component
    private Form updateShiftForm;
    
    @Component
    private Form removeShiftForm;
    
    
    void onActivate(Long id) {
    	try {
    		Shift shift = taskService.findShift(id);
    		updateShiftId = removeShiftId = id;
    		
    		permanent = Character.toString(shift.getPermanent());
    		
    		permanentTypes = new ArrayList<String>();
    		permanentTypes.add(Character.toString(GlobalNames.SHIFT_PERMANENT_NONE));
    		permanentTypes.add(Character.toString(GlobalNames.SHIFT_PERMANENT_MORNING));
    		permanentTypes.add(Character.toString(GlobalNames.SHIFT_PERMANENT_EVENING));
    		
    		startDate = shift.getStart().getTime();
        	Integer startHour = shift.getStart().get(Calendar.HOUR_OF_DAY);
        	Integer startMin = shift.getStart().get(Calendar.MINUTE);
        	startTime = formatTime(startHour.toString()) + ":" + formatTime(startMin.toString());
        	
        	endDate = shift.getEnd().getTime();
        	Integer endHour = shift.getEnd().get(Calendar.HOUR_OF_DAY);
        	Integer endMin = shift.getEnd().get(Calendar.MINUTE);
        	endTime = formatTime(endHour.toString()) + ":" + formatTime(endMin.toString());
    		
			} catch (InstanceNotFoundException e) {
		}
    }

    Object onValidateFromUpdateShiftForm() throws NumberFormatException, InstanceNotFoundException {

        if (!updateShiftForm.isValid()) {
            return this;
        }
        
        Shift shift = taskService.findShift(updateShiftId);
        
        String[] taskStartTime = startTime.split(":");
        Integer taskStartHour = Integer.parseInt(taskStartTime[0]);
        Integer taskStartMin = Integer.parseInt(taskStartTime[1]);
        Calendar shiftStart = Calendar.getInstance();
        shiftStart.setTime(startDate);
        if ((0 <= taskStartHour) && (taskStartHour <= 23) && (0 <= taskStartMin) && (taskStartMin <= 59)) {
	        shiftStart.set(Calendar.HOUR_OF_DAY, taskStartHour);
	        shiftStart.set(Calendar.MINUTE, taskStartMin);
        } else {
        	return this;
        }
        
        String[] taskEndTime = endTime.split(":");
        Integer taskEndHour = Integer.parseInt(taskEndTime[0]);
        Integer taskEndMin = Integer.parseInt(taskEndTime[1]);
        Calendar shiftEnd = Calendar.getInstance();
        shiftEnd.setTime(endDate);
        if ((0 <= taskEndHour) && (taskEndHour <= 23) && (0 <= taskEndMin) && (taskEndMin <= 59)) {
	        shiftEnd.set(Calendar.HOUR_OF_DAY, taskEndHour);
	        shiftEnd.set(Calendar.MINUTE, taskEndMin);
        } else {
        	return this;
        }
        
        if (shiftStart.before(shiftEnd)) {
        	try {
        		taskService.updateShift(shift.getShiftId(), shiftStart, shiftEnd, (char)permanent.charAt(0), shift.getManager().getUserProfileId());
        	} catch (InstanceNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        return StoreUMSchedule.class;
        
    }
    
    Object onValidateFromRemoveShiftForm() throws InstanceNotFoundException {
    	Shift shift = taskService.findShift(removeShiftId);
    	taskService.removeShift(shift.getShiftId());
    	return StoreUMSchedule.class;
    }
    
    private String formatTime(String s) {
    	if (s.length()<2) {
    		s="0"+s;
    	}
    	return s;
    }

}
